package com.xhwl.logistics.enums;

import com.xhwl.common.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Getter
@AllArgsConstructor
public enum VehiclePassTypeEnum {

    NORMAL(1, "正常放行"),
    EXCEPTION(2, "异常放行"),
    CHARGE(3, "收费放行"),
    PASS_TYPE_NOTING(99, "Noting_Pass_Type");

    private static final Map<Integer, VehiclePassTypeEnum> map;
    private static final Map<String, VehiclePassTypeEnum> mapForDescription;
    static {
        map = new HashMap<>();
        mapForDescription = new HashMap<>();
        for (VehiclePassTypeEnum vehiclePassTypeEnum : VehiclePassTypeEnum.values()) {
            map.put(vehiclePassTypeEnum.getId(), vehiclePassTypeEnum);
        }
        for (VehiclePassTypeEnum vehiclePassTypeEnum : VehiclePassTypeEnum.values()) {
            mapForDescription.put(vehiclePassTypeEnum.getDescription(), vehiclePassTypeEnum);
        }
    }

    private final Integer id;

    private final String description;

    public static VehiclePassTypeEnum toVehiclePassTypeEnum(Integer id) {
        return Optional.ofNullable(map.get(id)).orElse(PASS_TYPE_NOTING);
    }

    public static VehiclePassTypeEnum toVehiclePassTypeEnum(String description) {
        return Optional.ofNullable(mapForDescription.get(description))
                .orElseThrow(() -> new BusinessException("非法的description无法转换成VehiclePassTypeEnum"));
    }

    public static VehiclePassTypeEnum toVehiclePassTypeEnumByKeTop(Integer id) {
        //参考科拓的文档，把其放行类型相应转换为车行中台的进出类型
        if (2==id || 3==id || 4==id || 7==id || 8==id || 9==id || 10==id) {
            return EXCEPTION;
        } else {
            return NORMAL;
        }
    }

}
